import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * This is a simple dialog class used to modify or insert a single skill information.
 * 
 * @author zhongnanhai
 *
 */
public class SkillDialog extends Dialog{
	/**The selected skill*/
	private int skillId = -1;//-1 means invalid value
	/**The productivity value*/
	private float productivity = -1;//invalid value
	/**
	 * The constructor with a default style.
	 * @param parent its parent window
	 */
	public SkillDialog(Shell parent){
		this(parent,SWT.DIALOG_TRIM | SWT.CLOSE | SWT.PRIMARY_MODAL);
	}
	/**
	 * The constructor of SkillDialog.
	 * 
	 * @param parent its parent window
	 * @param style its specific style
	 */
	public SkillDialog(Shell parent, int style) {
		super(parent, SWT.DIALOG_TRIM | SWT.CLOSE | SWT.PRIMARY_MODAL | style);
		// TODO Auto-generated constructor stub
	}
	/**
	 * The open function of a dialog class.
	 */
	public void open(){
		Shell shell = new Shell(getParent(),getStyle());
		int x = getParent().getBounds().x + getParent().getBounds().width;
		int y = getParent().getBounds().y ;
		shell.setLocation(x,y);
		createContents(shell);
		shell.pack();
		shell.open();
		Display display = getParent().getDisplay();
		while(!shell.isDisposed()){
			if(!display.readAndDispatch())
				display.sleep();
		}
	}
	/**
	 * Create all the contents of the window.
	 * @param shell the skill dialog window
	 */
	private void createContents(final Shell shell){
		shell.setLayout(new GridLayout(2,true));
		Label nameLbl = new Label(shell,SWT.LEFT);
		nameLbl.setText("Name:");
		final Combo skillList = new Combo(shell,SWT.DROP_DOWN | SWT.READ_ONLY);
		skillList.setItems(AllBasicData.skills);
		Label prodLbl = new Label(shell,SWT.LEFT);
		prodLbl.setText("Productivity:");
		final Text prodTxt = new Text(shell,SWT.BORDER);
		prodTxt.setLayoutData(new GridData(GridData.FILL_BOTH));
		Button okBtn = new Button(shell,SWT.PUSH);
		okBtn.setText("OK");
		okBtn.setLayoutData(new GridData(GridData.FILL_BOTH));
		if(skillId >= 0){
			skillList.select(skillId);
			prodTxt.setText(productivity+"");
		}else{
			skillList.select(-1);
			prodTxt.setText("");
		}
		okBtn.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent e){
				if(skillList.getSelectionIndex()<0){
					Utilities.showMessage(shell,SWT.ICON_ERROR,"Please select a new skill for this employee");
					skillId = -1;
					return;
				}else if(prodTxt.getText().trim().isEmpty()){
					Utilities.showMessage(shell,SWT.ICON_ERROR,"Please set the productivity to this skill");
					productivity = -1;
					return;
				}else{
					skillId = skillList.getSelectionIndex();
					if(!prodTxt.getText().trim().isEmpty()){
						try{
							int pd = Integer.parseInt(prodTxt.getText());
							productivity = pd;
						}catch(Exception exp){
							productivity = -1;
							Utilities.showMessage(shell,SWT.ICON_ERROR,"Please set the correct number");
							return;
						}
					}
				}					
				shell.dispose();
			}
		});
		Button cancelBtn = new Button(shell,SWT.PUSH);
		cancelBtn.setText("CANCEL");
		cancelBtn.setLayoutData(new GridData(GridData.FILL_BOTH));
		cancelBtn.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent e){
				shell.dispose();
			}
		});
	}
	public int getSkillId() {
		return skillId;
	}
	public float getProductivity() {
		return productivity;
	}
	public void setSkillId(int skillId) {
		this.skillId = skillId;
	}
	public void setProductivity(float productivity) {
		this.productivity = productivity;
	}
}
